// Do file that creates the results for Tables 1, 2, 3, and A4 of the BJPS paper.
//
// Last changed: 2020-10-03 (KOL)
  
  clear all
  set more off 
  set matsize 10000
  
  capture log close
  local output "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy"
  log using "`output'\Output\Tables1_2_3_A4.log", replace text
  
  use "E:\ProjData\UtplaceringsData8594\utp-UtpData.dta", clear 
  
  merge 1:1 LopNr Ar using "E:\ProjData\UtplaceringsData8594\utp-EthnicContacts.dta"
  replace ArbAnd = 0 if ArbAnd == .
  
  replace mNom = mNom*100
  replace mVald = mVald*100
  
  rename mNom Nominerad
  rename mVald Vald
  destring sKommun, gen(KommunValAr) 
  
//Keep only refugee immigrants
  keep if FlyktInv == 1
    
//Recode earnings to monthly wages in 1000 SEK
  replace LoneInk = ((LoneInk*100)/12)/1000

  gen Kvinna = -1*(Kon-1)
  
//####################################### TABLE 1 #################################// 
  local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1"
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Kvinna Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
     
  listwise Alder Kvinna Gift_InvAr Barn0_15_InvAr UtbAr_InvAr logpEnklavInvAr Nominerad Vald  InvAlder  InvAr TidI InvAlder logpEnklavVuxenInvAr logpEnklavVuxen logpEnklav KommunValAr InKommun
  
  reghdfe Nominerad logpEnklavVuxen if `sample', absorb(KommunValAr `controls1') cluster(KommunValAr) old 
  gen sample = e(sample)
  
  sum logpEnklavVuxenInvAr if sample == 1, detail
  gen cEnklav=logpEnklavVuxenInvAr<r(p50) if logpEnklavInvAr <. 
  
  estpost tabstat Alder Kvinna Gift_InvAr Barn0_15_InvAr UtbAr_InvAr logpEnklavVuxen Nominerad Vald  InvAlder  InvAr if sample == 1, column(stat) statistics(mean sd)
  eststo d1

  estpost tabstat Alder Kvinna Gift_InvAr Barn0_15_InvAr UtbAr_InvAr logpEnklavVuxen Nominerad Vald  InvAlder  InvAr  if cEnklav==0 & sample == 1, column(stat) statistics(mean sd)
  eststo d2 
  esttab d1 d2, main(mean) aux(sd) nostar unstack nonote nomtitle b(2)

  estpost tabstat Alder Kvinna Gift_InvAr Barn0_15_InvAr UtbAr_InvAr logpEnklavVuxen Nominerad Vald  InvAlder  InvAr  if cEnklav==1 & sample == 1,  column(stat) statistics(mean sd)
  eststo d3

  esttab d1 d3 d2 using "`output'/Output/Table1.rtf", main(mean) aux(sd) nostar unstack nonote nomtitle b(2) ///
				rename(Alder Age Kvinna Female Gift_InvAr Married Barn0_15_InvAr "Nr of children under 16" ///
				 UtbAr_InvAr "Years of education" Nominerad "Nominated(%)" Vald "Elected(%)" ///
				InvAlder "Age at immigration" InvAr "Immigration year" logpEnklavVuxen "Ethnic density (log)") replace

//####################################### TABLE 2 #################################// 
local output "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy"
				
// Regression results Table 				
  local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1"
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Kvinna Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
  
  qui: reghdfe Nominerad logpEnklavVuxen if `sample', absorb(KommunValAr `controls1') cluster(KommunValAr) old
  keep if e(sample) == 1
  
  reghdfe Nominerad logpEnklavVuxen if `sample', absorb(KommunValAr `controls1') cluster(KommunValAr) old
  eststo c1 
  
  reghdfe Nominerad logpEnklavVuxen if `sample', absorb(InKommun `controls1') cluster(InKommun) old
  eststo c1b
  
preserve  
  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old 
  keep if e(sample) == 1
  sum logpEnklavVuxenInvAr, detail
  gen EnklavInvAr=logpEnklavVuxenInvAr > r(p50) if logpEnklavInvAr <.
  
  sum logpEnklavVuxen, detail
  gen Enklav=logpEnklavVuxen > r(p50) if logpEnklav <.
  
 compress
  //Drop one "singleton observation"
  save "E:\ProjData\PlacementCandidacy\utp-DataMainSample.dta", replace
restore   


  reghdfe Nominerad (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old 
  eststo c2
  
  reghdfe Nominerad `controls2' logpEnklavVuxen if `sample', absorb(`controls1' KommunValAr) cluster(KommunValAr) old
  eststo c3
  
  reghdfe Nominerad `controls2' logpEnklavVuxen if `sample', absorb(`controls1' InKommun) cluster(InKommun) old
  eststo c3b
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo c4
    
  esttab c1 c1b c2 c3 c3b c4 using "`output'\Output\Table2.rtf", keep(`controls2' logpEnklavVuxen) ///
		 replace star(* .1 ** .05 *** .01) b(3) se(3)
  
 //###################### Reduced form results for the appendix ####################//
 
  local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1"
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Kvinna Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
  
  reghdfe Nominerad logpEnklavVuxenInvAr if `sample', absorb(InKommun `controls1') cluster(InKommun) old 
  eststo rf1
  
  reghdfe Nominerad `controls2' logpEnklavVuxenInvAr if `sample', absorb(InKommun `controls1') cluster(InKommun) old
  eststo rf2
  
  esttab rf1 rf2 using "`output'\Output\Table2_RF.rtf", keep(`controls2' logpEnklavVuxenInvAr) ///
		 replace star(* .1 ** .05 *** .01) b(3) se(3)
  
  
//####################################### TABLE 3 #################################// 
 
  replace pEnklavVuxen =  pEnklavVuxen/100
  replace pEnklavVuxenInvAr = pEnklavVuxenInvAr/100
  
  preserve
  listwise `controls3'
  reghdfe Nominerad `controls2'  (logpEnklav=logpEnklavInvAr) if `sample', absorb(`controls1' InKommun) cluster(InKommun) stages(first) old
  sum Nominerad if e(sample)==1		
  eststo m3_1
  restore 
  
  reghdfe Nominerad `controls2' `controls3' (logpEnklav=logpEnklavInvAr) if `sample', absorb(`controls1' InKommun) cluster(InKommun) stages(first) old
  sum Nominerad if e(sample)==1		
  eststo m3_2

  reghdfe Nominerad `controls2' `controls3' PartSvensk (logpEnklav=logpEnklavInvAr) if `sample', absorb(`controls1' InKommun) cluster(InKommun) stages(first) old
  sum Nominerad if e(sample)==1		
  eststo m3_3

  reghdfe Nominerad `controls2' `controls3' PartSvensk ArbAndSvensk (logpEnklav=logpEnklavInvAr) if `sample', absorb(`controls1' InKommun) cluster(InKommun) stages(first) old
  sum Nominerad if e(sample)==1		
  eststo m3_4

  esttab m3_1 m3_2 m3_3 m3_4 using "`output'\Output\Table3.rtf", se(3) b(3) keep(logpEnklav UtbAr LoneInk Sysselsatt PartSvensk ArbAndSvensk) starlevels(* .1 ** .05 *** .001) ///
							 varlabels(UtbAr "Years of education" logpEnklav "Ethnic density (log)" ///
						     LoneInk "Earnings" Sysselsatt "Employed" PartSvens "Swedish partner" ArbAndSvensk "Share swedish colleagues" ) replace
	
  //####################################### TABLE A4 #################################// 
  reghdfe Nominerad `controls2' RMKvot (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old 
  eststo rc1
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & !inlist(InKommun, 180, 1280, 1480), absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo rc2
  
  reghdfe Nominerad `controls2' (logpEnklavVuxen = logpEnklavVuxenInvAr) if `sample' & inlist(IFAUkod, 31,  41, 46,47, 48), absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo rc3 
  
  xi: ivprobit Nominerad (logpEnklavVuxen=logpEnklavVuxenInvAr) `controls2' i.InKommun i.FodelseAr i.Ar i.InvAr i.IFAUkod if ///
			inrange(InvAr, 1987,1991) & Rostratt == 1 & inrange(InvAlder, 18,55) & inrange(Ar, 1991, 2014), first vce(cluster InKommun)
  eststo rc4	
 
  mfx_ivprob logpEnklavVuxen `controls2'
  mfx_ivprob logpEnklavVuxen `controls2', noendog
	
  replace pEnklavVuxen =  pEnklavVuxen*100
  replace pEnklavVuxenInvAr = pEnklavVuxenInvAr*100
	
  reghdfe Nominerad `controls2' (pEnklavVuxen = pEnklavVuxenInvAr) if `sample', absorb(InKommun `controls1') cluster(InKommun) stages(first) old
  eststo rc5 
  
  xi: ivprobit Nominerad (pEnklavVuxen=pEnklavVuxenInvAr) `controls2' i.InKommun i.FodelseAr i.Ar i.InvAr i.IFAUkod if ///
			inrange(InvAr, 1987,1991) & Rostratt == 1 & inrange(InvAlder, 18,55) & inrange(Ar, 1991, 2014), first vce(cluster InKommun)
  eststo rc6	
  
  mfx_ivprob pEnklavVuxen `controls2'
  mfx_ivprob pEnklavVuxen `controls2', noendog
  
  local output "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy"
  esttab rc2 rc3 rc4 rc5 rc6 using "`output'\Output\TableA4.rtf", keep(`controls2' logpEnklavVuxen pEnklavVuxen) ///
		 replace star(* .1 ** .05 *** .01) b(3) se(3)
		 
  log close 
